package cn.kgc.vue.service.impl;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.DeptPageDTO;
import cn.kgc.vue.entity.Dept;
import cn.kgc.vue.mapper.DeptMapper;
import cn.kgc.vue.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;


@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>  implements DeptService{

    @Resource
    private DeptMapper deptMapper;

    @Override
    public ResponseResult getPage(DeptPageDTO deptPageDTO) {

        Page<Dept> deptPage = new Page<>(deptPageDTO.getCurrentPage(), deptPageDTO.getLimit());
        LambdaQueryWrapper<Dept> lambda = new QueryWrapper<Dept>().lambda();
        lambda.eq(StringUtils.isNotBlank(deptPageDTO.getDeptName()),Dept::getDeptName,deptPageDTO.getDeptName())
        .orderByDesc(Dept::getId);
        deptMapper.selectPage(deptPage,lambda);

        //封装分页数据 返回前端
        ResponseResult responseResult = new ResponseResult();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",deptPage.getTotal());
        map.put("currentPage",deptPage.getRecords());
        responseResult.success(ResponseStatus.SUCCESS,map);

        return responseResult;
    }

    @Override
    public ResponseResult saveOrUpdateDept(Dept dept) {

        return null;
    }
}




